<?php namespace Home\Controller; 
//详情页控制器

//品牌表
use \Common\Model\Brand;
//分类表
use \Common\Model\Cate;
//商品属性表
use \Common\Model\GoodsAttr;
//商品表
use \Common\Model\Goods;
//类型属性表
use \Common\Model\TypeAttr;
//货号表
use \Common\Model\GoodsList;
class ContentController extends CommonController{
    private $Model;
//  自定义构造函数
    public function __auto(){
        //引Model文件					商品表
        $this->Model = new \Home\Model\Content;
    } 
    
//  首页
    public function index(){
    	//商品表&商品属性表&商品详细表
    	$aid = Q("get.aid",0,'intval');
//		$goodsData = Db::select("select * from goods as gd join goods_attr as ga on gd.aid=ga.goods_aid join shop_detail as sd on gd.aid=sd.goods_aid group By aid");			
		$goodsData = Db::table('goods')
						->join('goods_attr as ga','goods.aid','=','ga.goods_aid')
						->join('shop_detail as sd','goods.aid','=','sd.goods_aid')
						->groupBy('aid')
						->where("aid={$aid}")
						->first();
//		p($goodsData);
		$goodsData['images'] = explode(',', $goodsData['images']);
		foreach ($goodsData['images'] as $k => $v) {
			$thumb = pathinfo($v);
			$data = [];
			$data[] = str_replace('.'.$thumb['extension'], '_mid.'.$thumb['extension'], $v);
			$data[] = str_replace('.'.$thumb['extension'], '_big.'.$thumb['extension'], $v);
			$goodsData['images'][$k] = $data;
		}
//		p($goodsData);
		View::with('goodsData',$goodsData);
		//品牌表
		$brandModel = new Brand();
		$bid = Q("get.bid",NULL,'intval');
		$brandData = $brandModel->where("bid={$bid}")->find();
		$logo = pathinfo($brandData['logo']);
//		p($logo);
		$brandData['logo']=str_replace('_thumb', '', $brandData['logo']);
//		p($brandData);
		View::with('brandData',$brandData);

		
//	相关分类
//		1.获取顶级分类
		$cid = Q("get.cid",0,'intval');
		$cateModel = new Cate();
		$cateData = $cateModel->where("pid=0")->get();
//		2.通过当前的cid获得所有的子集id
		$cids = $cateModel->getSon($cateModel->get(),$cid);
		$cids[] = $cid;
//		p($cids);
//		3.获得所有子集分类
		$cateData = $cateModel
							->where("cid IN(". implode(',', $cids).")")
							->orderBy('rand()')
							->limit(6)
							->get();
//		p($cateData);
		View::with('cateData',$cateData);
		
//	推广商品
		$goodsModel = new Goods();
		$goods = $goodsModel
				->orderBy("rand()")
				->limit(10)
				->get();
//		p($goods);
		View::with('goods',$goods);
		
		//商品详情表
		$goodsAttrModel = new GoodsAttr();
		$goodsAttrData = $goodsAttrModel
						->join("type_attr",'type_attr_taid','=','taid')
						->where("goods_aid={$aid} AND class=0")->get();
//		p($goodsAttrData);
		View::with('goodsAttrData',$goodsAttrData);
		
	
//	选取规格
//		通过属性表的taid查找类型表
		$typeAttrModel = new TypeAttr();
		//获得该类型的规格   并且  规格为1
		$TypeAttrData = $typeAttrModel->where("type_tid={$goodsData['type_tid']}  AND class=1")->get();
		//商品属性表		该商品的类型
		foreach ($TypeAttrData as $k => $v) {
			$TypeAttrData[$k]['spec'] = Db::table('goods_attr')
										->where("type_attr_taid={$v['taid']} AND goods_aid={$goodsData['aid']}")
										->get();
		}
//		p($TypeAttrData);
		View::with('TypeAttrData',$TypeAttrData);
        View::make();
		
    }
	//选中的商品
	public function getAttr(){
//	    p(Q("post.en"));
//		exit;
		//通过商品属性id找到商品
		$data = Q("post.en");
//		echo $data['combine'];
//		exit;
		$goodsListModel = new GoodsList();
		$data = $goodsListModel->where("combine='{$data['combine']}' AND goods_aid={$data['aid']}")->find();
		if($data){
			echo json_encode($data);
			exit;
		}else{
			echo 0;
			exit;
		}
		
	}
	//异步
	public function getShop(){
		$data = Q("post.data");
		$goodsListModel = new GoodsList();
		$glid = $goodsListModel->where("combine='{$data['combineNum']}' AND goods_aid={$data['aid']}")->find();		
		if($glid){
		    $data = [ 
			    'id'        => $data['aid'], // 商品 ID 
			    'name'      =>"{$data['title']}",// 商品名称 
			    'num'       => $data['val'], // 商品数量 
			    'price'     => $data['price'], // 商品价格	
			    'options'   => [
			    	'combine'=>$data['combine'],
			    	'bname'=>$data['bname'],
			    	'src'=>$data['src'],
			    	'marketprice'=>$data['marketprice'],
			    	'number'=>$data['number'],
			    	'bid'=>$data['bid'],
			    	'glid'=>$glid
			    	]// 其他参数如价格、颜色、可以为数组或字符串 
			];
			Cart::add($data); // 添加到购物车 
			echo 1;
		}else{
			echo 0;
		}
	}


}